var listPage = avalon.define({
	$id: "listPage",
	uid: "",
	pos: [],
	noPOData: "",
	p_pageNo: 1,
	noCIData: "",
	c_pageNo: 1,
	cis: [],
	flag: ""
});

mui.ready(function() {
	
	
	$("#search").focus();
	var slider = mui('#slider').slider();
	var mui_po = mui('#po-list');
	var mui_ci = mui('#ci-list');
	listPage.uid = $('#uid').val();
	var q = $("#search").val();
	
	if(""==q){
		listPage.flag="";
	}else{
		listPage.flag="1";
	}

	var mui_po = mui('#po-list');
	mui_po.pullRefresh({
		container: "#po-list",
		up: {
			height: 50,
			auto: true,
			contentrefresh: $.i18n.contentRefresh,
			contentnomore: $.i18n.noData,
			callback: function() {
				setTimeout(function() {
					mui.ajax('/sign/search.do', {
						data: {
							q: q,
							uid: listPage.uid,
							type: 1,
							pageNo: listPage.p_pageNo
						},
						dataType: 'json',
						success: function(items) {
							var pos = listPage.pos;
							
							avalon.each(items, function(i, item) {
								pos.push(item);
							});
							listPage.pos = pos;
							mui_po.pullRefresh().endPullupToRefresh(false);
							if(listPage.pos.length == 0) {
								listPage.noPOData = "<img src='../../resources/images/noDataImg.png'><p>No data</p>";
							}
							if(items.length > 0) {
								listPage.p_pageNo = listPage.p_pageNo + 1;
							}

						}
					});

				}, window.DAHUA_CONFIG.LOADING_TIME);

			}
		}

	});

	var mui_ci = mui('#ci-list');
	mui_ci.pullRefresh({
		container: "#ci-list",
		up: {
			height: 50,
			auto: true,
			contentrefresh: $.i18n.contentRefresh,
			contentnomore: $.i18n.noData,
			callback: function() {
				listPage.noCIData = "";
				setTimeout(function() {
					var q = $("#search").val();
					mui.ajax('/sign/search.do', {
						data: {
							q: q,
							uid: listPage.uid,
							type: 2,
							pageNo: listPage.c_pageNo
						},
						dataType: 'json',
						success: function(items) {
							var cis = listPage.cis;
							avalon.each(items, function(i, item) {
								cis.push(item);
							});
							listPage.cis = cis;
							mui_ci.pullRefresh().endPullupToRefresh(false);
							if(listPage.cis.length == 0) {
								listPage.noCIData = "<img src='../../resources/images/noDataImg.png'><p>No data</p>";
							}
							if(items.length > 0) {
								listPage.c_pageNo = listPage.c_pageNo + 1;
							}

						}
					});

				}, window.DAHUA_CONFIG.LOADING_TIME);

			}
		}

	});
	
	mui(".mui-scroll").on('tap', '.open-pdpPO', function() {
		var signId = this.getAttribute('id');
		var status = this.getAttribute('status');
		if(status != 1) {
			mui.openWindow({
				url: "pdp.html" + "?signId=" + signId
			});
		}else{
			mui.openWindow({
				url: "pdp.html" + "?signId=" + signId + "&isReceipted=" + 1
			});
		}
		
	});
	
	mui(".mui-scroll").on('tap', '.open-pdpCI', function() {
		var signId = this.getAttribute('id');
		var status = this.getAttribute('status');
		if(status != 1) {
			mui.openWindow({
				url: "pdp.html" + "?signId=" + signId
			});
		}else{
			mui.openWindow({
				url: "pdp.html" + "?signId=" + signId + "&isReceipted=" + 1
			});
		}
		
	});

	document.getElementById("search").addEventListener('keyup', function(event) {
		var e = event || window.event || arguments.callee.caller.arguments[0];
		if(e && e.keyCode == 13) {
			listPage.flag = "1";
			listPage.noCIData = "";
			listPage.noPOData = "";
			var q =$("#search").val();
			if(""==q){
				listPage.flag="";
				listPage.pos=[];
				listPage.cis = [];
				mui.toast("Please enter keyword");
				$("#search").focus();
			}else{
				mui.ajax('/sign/search.do', {
					data: {
						q: q,
						uid: listPage.uid,
						type: 1,
						pageNo: 1
					},
					dataType: 'json',
					success: function(items) {
						if(items.length == 0) {
							listPage.noPOData = "<img src='../../resources/images/noDataImg.png'><p>No data</p>";
							listPage.pos=[];
							slider.gotoItem(1);
						} else {
							listPage.pos = items;
							listPage.p_pageNo = listPage.p_pageNo + 1;
						}
					}
				});
				mui.ajax('/sign/search.do', {
					data: {
						q: q,
						uid: listPage.uid,
						type: 2,
						pageNo: 1
					},
					dataType: 'json',
					success: function(items) {
						if(items.length == 0) {
							listPage.noCIData = "<img src='../../resources/images/noDataImg.png'><p>No data</p>";
							listPage.cis = [];
							slider.gotoItem(0);
						} else {
							listPage.cis = items;
							listPage.c_pageNo = listPage.c_pageNo + 1;
						}
					}
				});
			}
		}
	});
});